package com.atguigu.search.controller;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class SearchSaveController {
    @Autowired
    private SearchService searchService;
    @GetMapping("/test")
    public R test(){
        System.out.println("这是测试");
        return R.ok().put("data","这是测试");
    }

    @PostMapping("/product")
    public R searchSaveProduct(@RequestBody List<SkuEsModel> skuEsModels){
        Boolean b=false;
        try {
            b=searchService.saveProduct(skuEsModels);
        } catch (IOException e) {
            log.error("SearchSaveController上架商品发生错误");
            return R.error(BizCodeEnum.PRODUCT_UP.getCode(),BizCodeEnum.PRODUCT_UP.getMsg());
        }
        if (b){
            return R.ok();
        }else {
            return R.error(BizCodeEnum.PRODUCT_UP.getCode(),BizCodeEnum.PRODUCT_UP.getMsg());
        }

    }
}
